package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理控制器
 */
@RestController
@RequestMapping("/depts")
public class DeptController {

    //日志对象
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result findAll() {
        log.info("查询所有部门信息");
        //需要List<Dept> 数据 需要service来完成
        List<Dept> list = deptService.findAll();
        log.info("查询结果：{}",list);
        return Result.success(list);
    }

    @DeleteMapping
    public Result delete(Integer id) {
        log.info("根据id删除部门信息，id：{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Dept dept) {
        log.info("新增部门信息，参数：{}",dept);
        deptService.save(dept);
        return Result.success();
    }

    //根据id查询部门信息
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("根据id查询部门信息，id：{}",id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门信息，参数：{}",dept);
        deptService.update(dept);
        return Result.success();
    }

}
